/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/initial',
    component: () => import('@/views/initial/index'),
    meta: {
      title: '设置wsdl信息',
      hidden: true,
    },
  },
]

export const asyncRoutes = [
  {
    path: '/',
    name: 'BloodMatch',
    component: Layout,
    meta: {
      title: '配血管理',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'BloodMatchIndex',
        component: () => import('@/views/bloodMatch/index'),
        meta: {
          title: '配血管理',
          noClosable: true,
          keepAlive: true,
        },
      },
      {
        path: 'specimen',
        name: 'BloodMatchSpecimen',
        component: () => import('@/views/bloodMatch/specimen/index'),
        meta: {
          title: '标本检验',
        },
      },
      {
        path: 'makeup',
        name: 'BloodMatchMakeup',
        component: () => import('@/views/bloodMatch/makeup/index'),
        meta: {
          title: '补打配血单',
          keepAlive: true,
        },
      },
    ],
  },
  {
    path: '/bloodApply',
    name: 'Root',
    component: Layout,
    meta: {
      title: '用血申请',
      breadcrumbHidden: true,
    },
    redirect: '/bloodApply',
    children: [
      {
        path: 'index',
        name: 'BloodApplyIndex',
        component: () => import('@/views/bloodApply/index'),
        meta: {
          title: '用血申请单',
        },
      },
    ],
  },
  {
    path: '/bloodStock',
    name: 'BloodStock',
    component: Layout,
    meta: {
      title: '血液库存管理',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'stock',
        name: 'BloodStockStock',
        component: () => import('@/views/bloodStock/stock/index'),
        meta: {
          title: '血液库存管理',
        },
      },
      {
        path: 'buy',
        name: 'BloodStockBuy',
        component: () => import('@/views/bloodStock/buy/index'),
        meta: {
          title: '外买血液接收',
        },
      },
      // {
      //   path: 'collect',
      //   name: 'BloodStockCollect',
      //   component: () => import('@/views/bloodStock/collect/index'),
      //   meta: {
      //     title: '自采血液接收',
      //   },
      // },
      {
        path: 'index',
        name: 'BloodStockIndex',
        component: () => import('@/views/bloodStock/index/index'),
        meta: {
          title: '血液信息管理',
        },
      },
      {
        path: 'outbound',
        name: 'BloodOutboundIndex',
        component: () => import('@/views/bloodStock/outbound/index'),
        meta: {
          title: '血液报废管理',
        },
      },
      // {
      //   path: 'his',
      //   name: 'BloodHisIndex',
      //   component: () => import('@/views/bloodStock/his/index'),
      //   meta: {
      //     title: 'HIS血液拉取',
      //   },
      // },
    ],
  },
  {
    path: '/labTest',
    name: 'LabTest',
    component: Layout,
    meta: {
      title: '实验室检测',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'recheck',
        name: 'LabTestRecheck',
        component: () => import('@/views/labTest/recheck/index'),
        meta: {
          title: '献血者血型复检',
        },
      },
    ],
  },
  {
    path: '/inspect',
    name: 'Inspect',
    component: Layout,
    meta: {
      title: '机器检验相关',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'machine',
        name: 'InspectMachine',
        component: () => import('@/views/inspect/machine/index'),
        meta: {
          title: '机器检验数据',
        },
      },
      {
        path: 'abo',
        name: 'InspectAbo',
        component: () => import('@/views/inspect/abo/index'),
        meta: {
          title: '血型复检信息',
        },
      },
      {
        path: 'xm',
        name: 'InspectXm',
        component: () => import('@/views/inspect/xm/index'),
        meta: {
          title: '配血检验信息',
        },
      },
      {
        path: 'specimen',
        name: 'SpecimenAbo',
        component: () => import('@/views/inspect/specimen/index'),
        meta: {
          title: '标本检验信息',
        },
      },
    ],
  },
  {
    path: '/system',
    name: 'System',
    component: Layout,
    meta: {
      title: '系统设置',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'setting',
        name: 'SystemSetting',
        component: () => import('@/views/system/setting/index'),
        meta: {
          title: '系统设置',
        },
      },
      {
        path: 'machine',
        name: 'SystemMachine',
        component: () => import('@/views/system/machine/index'),
        meta: {
          title: '机器管理',
        },
      },
      {
        path: 'bloodSource',
        name: 'SystemBloodSource',
        component: () => import('@/views/system/bloodSource/index'),
        meta: {
          title: '血源管理',
        },
      },
      {
        path: 'product',
        name: 'SystemProduct',
        component: () => import('@/views/system/product/index'),
        meta: {
          title: '血液成分管理',
        },
      },
      {
        path: 'bloodCode',
        name: 'SystemBloodCode',
        component: () => import('@/views/system/bloodCode/index'),
        meta: {
          title: '血液成分码管理',
        },
      },
      {
        path: 'fetch',
        name: 'SystemFetch',
        component: () => import('@/views/system/fetch/index'),
        meta: {
          title: '接口测试',
        },
      },
      {
        path: 'print',
        name: 'SystemPrint',
        component: () => import('@/views/system/print/index'),
        meta: {
          title: '打印测试',
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter()

function fatteningRoutes(routes) {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

export function resetRouter(routes = constantRoutes) {
  routes.map((route) => {
    if (route.children) {
      route.children = fatteningRoutes(route.children)
    }
  })
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
